<?php
class ModelSite extends Model {
	public function getSites($data) {
        
        $sql  = "SELECT * FROM " . DB_PREFIX . "site ";
        if(isset($data['category']))
        {
            $sql .= ' where cat_id = ' . $this->db->escape($data['category'])." ";
        }
        
		$sort_data = array(
			'site_id',
			'site_name',
			'site_url',
			'pagerank',
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY site_id ";	
		}
        
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
        if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}		
        $query = $this->db->query($sql);
        $site_data = $query->rows;
		return $site_data;
	}
    public function getSiteCatName($site_id)
    {
        $query = $this->db->query("SELECT cat_id FROM " . DB_PREFIX . "site where site_id='$site_id'");
        $cat_id= $query->row['cat_id'];
        
        $query = $this->db->query("SELECT cat_name FROM " . DB_PREFIX . "category where cat_id='$cat_id'");
        $cat_name = $query->row['cat_name'];
        return $cat_name;
    }
    
    public function getTotalSites($data=array())
    {
        
        $sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "site` WHERE  status > '0' ";
        if(isset($data['category']))
        {
            $sql .= ' and cat_id = ' . $this->db->escape($data['category']);
        }
        
      	$query = $this->db->query($sql);
		
		return $query->row['total'];
    }
    
    public function getSiteInfo($site_id)
    {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "site where site_id='$site_id'");
        $info = $query->row;
        
        return $info;
    }
    
    public function insert($data)
    {
        $query  = $this->db->query("INSERT INTO " . DB_PREFIX . "site SET  site_name = '" . $this->db->escape($data['site_name']) . "', site_url = '" .$this->db->escape($data['site_url']) . "', cat_id = '" . $this->db->escape($data['cat_id']) . "', account='" . $this->db->escape($data['account']) . "', other='" . $this->db->escape($data['other']) . "', status=1 ");
        $cat_id = $this->db->getLastId();
        return $cat_id;
    } 
    public function update($data)
    {
        $query  = $this->db->query("UPDATE " . DB_PREFIX . "site SET  site_name = '" . $this->db->escape($data['site_name']) . "', site_url = '" .$this->db->escape($data['site_url']) . "', cat_id = '" . $this->db->escape($data['cat_id']) . "', account='" . $this->db->escape($data['account']) . "', other='" . $this->db->escape($data['other']) . "' WHERE site_id='" . $this->db->escape($data['site_id']) . "'");
    }
    
    public function delete($site_id)
    {
        $query = $this->db->query("DELETE FROM " . DB_PREFIX . "site  where site_id='".(int)$site_id ."'");
        return true;
    }
    
    public function check($site_url)
    {
        $query = $this->db->query("SELECT count(*) as num  FROM " . DB_PREFIX . "site where site_url like '%$site_url%'");
        
        if($query->row['num'])
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public function changeCategory($site_id,$cat_id=1)
    {
        $query  = $this->db->query("UPDATE " . DB_PREFIX . "site SET  cat_id = '" . $this->db->escape($cat_id) ."'  where site_id='" . $this->db->escape($site_id) ."'");
    }    
    
    public function updatePageRank($site_id,$pagerank=0)
    {
        $query  = $this->db->query("UPDATE " . DB_PREFIX . "site SET  pagerank = '" . $this->db->escape($pagerank) ."'  where site_id='" . $this->db->escape($site_id) ."'");
    }
    
    public function getPageRankCount($pagerank=3)
    {
        $sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "site` WHERE  pagerank = ".(int)$pagerank ."";        
      	$query = $this->db->query($sql);
		return $query->row['total'];
    }
}
?>